package com.fenger.sys.controller;


import com.fenger.sys.contast.SysConstast;
import com.fenger.sys.domain.Permission;
import com.fenger.sys.domain.User;
import com.fenger.sys.service.PermissionService;
import com.fenger.sys.service.RoleService;
import com.fenger.sys.utils.*;
import com.fenger.sys.vo.PermissionVo;
import com.fenger.sys.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class RoleController {
    private final static String ROLE_PREFIX = "role/";

    @Autowired
    RoleService roleService;

    @Autowired
    PermissionService permissionService;


    @ResponseBody
    @RequestMapping(ROLE_PREFIX + "loadMenuForRoleManager")
    public DtreeJson loadAllLeftMenuForRoleManager(RoleVo roleVo) {
        PermissionVo permissionVo = new PermissionVo();
        HttpSession session = WebUtils.getCurrentSession();
        permissionVo.setAvailable(SysConstast.AVAILABLE_TRUE);
//        permissionVo.setType(SysConstast.TYPE_PERMISSION_MENU);
        User user = (User) session.getAttribute("user");
        List<Permission> permissions = null;
//        if (user.getType() == SysConstast.USER_SUPER) {
//            permissions = permissionService.queryAllPermission(permissionVo);
//        } else {
            permissions = permissionService.queryPermissionByUserid(permissionVo, user.getId());
//        }

        //查询角色拥有的权限
        List<Permission> p = this.permissionService.queryPermissionByRoleId(roleVo.getId());
        List<DtreeNode> dtreeNodes = new ArrayList<>();
        for (Permission permission : permissions) {
            Integer id = permission.getId();
            String title = permission.getName();

            String checkArr = "0";
            for (Permission p1 : p) {
                if(p1.getId()==permission.getId()){
                    checkArr = "1";
                    break;
                }
            }
            Integer parentId = permission.getPid();
            Boolean spread = permission.getOpen() == SysConstast.STATUS_OPEN_TRUE ? true : false;
            String iconClass = permission.getIcon();
            dtreeNodes.add(new DtreeNode(id, title, checkArr, parentId, spread, iconClass));
        }
        List<DtreeNode> nodes = DtreeNodeUtils.build(dtreeNodes, 0);
        return new DtreeJson(SysConstast.SUCCESS_CODE, SysConstast.SUCCESS_MSG, nodes);
    }

    /**
     * 为分配角色的下拉树加载数据 包含选中信息
     * @param roleVo
     * @return
     */
    @ResponseBody
    @RequestMapping(ROLE_PREFIX + "loadPermissionMenuForXlTree")
    public DtreeJson loadPermissionMenuForXlTree(RoleVo roleVo) {
        PermissionVo permissionVo = new PermissionVo();
        permissionVo.setAvailable(SysConstast.AVAILABLE_TRUE);
//        permissionVo.setType(SysConstast.TYPE_PERMISSION_MENU);
        List<Permission> permissions = null;
//        if (user.getType() == SysConstast.USER_SUPER) {
//            permissions = permissionService.queryAllPermission(permissionVo);
//        } else {
        //查询出所有的权限 permission和菜单menu
        permissions = permissionService.queryAllPermission(permissionVo);
//        }

        //查询角色拥有的权限 permission和菜单menu
        List<Permission> p = this.permissionService.queryPermissionByRoleId(roleVo.getId());
        List<DtreeNode> dtreeNodes = new ArrayList<>();
        for (Permission permission : permissions) {
            Integer id = permission.getId();
            String title = permission.getName();

            String checkArr = "0";
            for (Permission p1 : p) {
                if(p1.getId()==permission.getId()){
                    checkArr = "1";
                    break;
                }
            }

            Integer parentId = permission.getPid();
            Boolean spread = permission.getOpen() == SysConstast.STATUS_OPEN_TRUE ? true : false;
            String iconClass = permission.getIcon();
            dtreeNodes.add(new DtreeNode(id, title, checkArr, parentId, spread, iconClass));
        }
        List<DtreeNode> nodes = DtreeNodeUtils.build(dtreeNodes, 0);

        return new DtreeJson(SysConstast.SUCCESS_CODE, SysConstast.SUCCESS_MSG, nodes);
    }

    @ResponseBody
    @RequestMapping(ROLE_PREFIX + "loadAllRole")
    private DataGirdView loadAllRole(RoleVo roleVo) {
        return this.roleService.queryAllRole(roleVo);
    }

      @ResponseBody
    @RequestMapping(ROLE_PREFIX + "queryRoleByUserid")
    private DataGirdView queryRoleByUserid(Integer id) {
        return this.roleService.queryRoleByUserid(id);
    }


     @ResponseBody
    @RequestMapping(ROLE_PREFIX + "dispatcherPermissionForRole")
    private DataGirdView dispatcherPermissionForRole(RoleVo roleVo) {
        return this.permissionService.dispatcherPermission(roleVo);
    }

    @ResponseBody
    @RequestMapping(ROLE_PREFIX + "addRole")
    private DataGirdView addRole(RoleVo roleVo) {
        roleVo.setCreatetime(new Date());
        try {
            this.roleService.addRole(roleVo);
            return new DataGirdView(SysConstast.ADD_SUCCESS_CODE, SysConstast.ADD_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.ADD_ERROR_CODE, SysConstast.ADD_ERROR_MSG, 0L, null);
        }
    }

    @ResponseBody
    @RequestMapping(ROLE_PREFIX + "updateRole")
    private DataGirdView updateRole(RoleVo roleVo) {

        try {
            this.roleService.updateRole(roleVo);
            return new DataGirdView(SysConstast.UPDATE_SUCCESS_CODE, SysConstast.UPDATE_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.UPDATE_ERROR_CODE, SysConstast.UPDATE_ERROR_MSG, 0L, null);
        }
    }

    @ResponseBody
    @RequestMapping(ROLE_PREFIX + "deleteRole")
    private DataGirdView deleteRole(RoleVo roleVo) {

        try {
            this.roleService.deleteRole(roleVo.getId());
            return new DataGirdView(SysConstast.DELETE_SUCCESS_CODE, SysConstast.DELETE_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.DELETE_ERROR_CODE, SysConstast.DELETE_ERROR_MSG, 0L, null);
        }
    }


    @ResponseBody
    @RequestMapping(ROLE_PREFIX + "deleteListRole")
    private DataGirdView deleteListRole(@RequestParam(value = "id[]", required = false) String[] id) {
        System.out.println("--------------------");
        System.out.println(id);
        for (int i = 0; i < id.length; i++) {
            String s = id[i];
            System.out.println(s);
        }
        return new DataGirdView(SysConstast.ERROR_CODE, SysConstast.DELETE_ERROR_MSG, 0L, null);
    }

}
